package com.bw.goods_module.bind;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bw.goods_module.R;
import com.bw.goods_module.home.GoodsEntity;
import com.bw.goods_module.home.adapter.GoodsAdapter;
import com.bw.imgload_sdk.LoadImgUtils;
import com.bw.widget_sdk.decoration.HomeGoodsDecoration;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.List;

public class DetailBind {


    @BindingAdapter(value = {"goodsDetail","goodsList","detailitemclick"})
    public static void bindDetail(RecyclerView recyclerView,
                                  GoodsEntity goodsEntity,
                                  List<GoodsEntity>goodsList,
                                  OnItemClickListener detailitemclick){
        GoodsAdapter adapter = (GoodsAdapter) recyclerView.getAdapter();
        if (adapter==null){
            View headView = LayoutInflater.from(recyclerView.getContext())
                    .inflate(R.layout.view_homevalue,null,false);
            adapter = new GoodsAdapter();
            adapter.setOnItemClickListener(detailitemclick);
            GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),2);
            manager.setOrientation(GridLayoutManager.VERTICAL);
            recyclerView.addItemDecoration(new HomeGoodsDecoration(recyclerView.getContext()));
            recyclerView.setLayoutManager(manager);
            adapter.addHeaderView(headView);
            recyclerView.setAdapter(adapter);
        }else {
            View headView = adapter.getHeaderLayout();
            ImageView value_head_img = headView.findViewById(R.id.value_head_img);
            TextView value_head_title = headView.findViewById(R.id.value_head_title);
            TextView value_head_dec = headView.findViewById(R.id.value_head_dec);
            TextView mSkuLabelTv = headView.findViewById(R.id.mSkuLabelTv);
            LogUtils.e("重置绑定数据");
            if (goodsEntity!=null){
                //添加商品详情信息
                LoadImgUtils.loadMatchImg(goodsEntity.goods_default_icon,value_head_img);
                value_head_title.setText(goodsEntity.goods_desc);
                value_head_dec.setText(goodsEntity.goods_default_price);
                mSkuLabelTv.setText(goodsEntity.goods_default_sku);
            }
            if (goodsList!=null){
                //添加商品列表信息
                adapter.setNewInstance(goodsList);
            }
        }
    }
}
